package com.ditri.distrident.common.utils;

import com.ditri.distrident.common.constant.CommonConst;

import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Collection;
import java.util.Map;

/**
 * @author: edxuanlen
 * @date: 2020-08-03 17:07
 * @version: 1.0
 **/

@SuppressWarnings("rawtypes")
public class CommonUtil {

    private static String toString(Object object) {
        return object == null ? "" : object.toString();
    }

    public static boolean isEmpty(Collection collection) {
        return collection == null || collection.isEmpty();
    }

    public static boolean isEmpty(Map map) {
        return map == null || map.isEmpty();
    }

    public static boolean isEmpty(String string) {
        return toString(string).isEmpty();
    }

    public static boolean isEmptyTrim(String string) {
        return toString(string).trim().isEmpty();
    }

    public static boolean isEmpty(Object object) {
        return toString(object).isEmpty();
    }

    public static boolean isEmptyTrim(Object object) {
        return toString(object).trim().isEmpty();
    }

    public static <T> boolean isEmpty(T[] array) {
        return array == null || array.length == 0;
    }


    /*************************** 日历 ********************************/
    /**
     * 获取一个日期的 时间戳
     *
     * @return 时间单位[ms]
     */
    public static Long getCalendar(int year, int month, int dayOfYear, int hour, int minute, int second) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_YEAR, dayOfYear);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.SECOND, second);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        return cal.getTimeInMillis();
    }

    public static Long getCalendar(int year) {
        return getCalendar(year, 1, 1, 0, 0, 0);
    }

    public static Long getZeroHour() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    /**
     * 获取用户id
     * @param username 用户名
     * @return hash后的id
     */
    public static String getOnlyId(String username) {

        byte[] bytes = username.getBytes(StandardCharsets.UTF_8);
        StringBuilder stringBuilder = new StringBuilder();
        for(byte b: bytes){
            stringBuilder.append(b);
        }

        int prefix = stringBuilder.hashCode();

        return CommonConst.Blog.PREFIX + prefix + "-" + System.currentTimeMillis();
    }


}